package cn.javass.dp.state.example3;

import java.util.HashMap;
import java.util.Map;

public class VoteManager {

    private VoteState state = null;

    //记录用户投票结果
    private Map<String, String> mapVote = new HashMap<>();
    //记录用户投票次数
    private Map<String, Integer> mapVoteCount = new HashMap<>();

    public Map<String, String> getMapVote() {
        return mapVote;
    }

    public void setMapVote(Map<String, String> mapVote) {
        this.mapVote = mapVote;
    }

    public Map<String, Integer> getMapVoteCount() {
        return mapVoteCount;
    }

    public void setMapVoteCount(Map<String, Integer> mapVoteCount) {
        this.mapVoteCount = mapVoteCount;
    }

    public void vote(String user, String voteItem) {
        Integer oldVoteCount = mapVoteCount.get(user);
        if (oldVoteCount == null) {
            oldVoteCount = 0;
        }
        oldVoteCount = oldVoteCount + 1;
        mapVoteCount.put(user,oldVoteCount);

        if (oldVoteCount ==1) {
           state = new NormalVoteState();
        } else if (oldVoteCount > 1 && oldVoteCount < 5) {
            System.out.println(" 请不要重复投票");

        } else if (oldVoteCount >= 5 && oldVoteCount <= 8) {

            System.out.println("恶意刷票，禁止投票");
        } else if (oldVoteCount > 8) {
            state = new BlackVoteState();
        }
        state.vote(user, voteItem , this);
    }
}
